class BroadElement {
	private static final int MAX_BREADTH=10;
	private BroadElement parent;
	private int id;
	private int order;
	private int breadth;
	private int refs;
	
	private BroadElement next1;
	private BroadElement next2;
	private BroadElement next3;
	private BroadElement next4;
	private BroadElement next5;
	private BroadElement next6;
	private BroadElement next7;
	private BroadElement next8;
	private BroadElement next9;
	private BroadElement next10;
	
	BroadElement(int id, int breadth, BroadElement prev, int index) {
		this.id = id;
		this.parent = prev;
		this.order = index;
		this.breadth = breadth;
		this.refs = 0;
		
		this.next1 = null;
		this.next2 = null;
		this.next3 = null;
		this.next4 = null;
		this.next5 = null;
		this.next6 = null;
		this.next7 = null;
		this.next8 = null;
		this.next9 = null;
		this.next10 = null;
	}	
	
	static int getMaxBreadth() {
		return MAX_BREADTH;
	}
	
	static int validateBreadth(int breadth) {
		if(breadth < 0 || breadth > MAX_BREADTH) {
			return MAX_BREADTH;
		}
		return breadth;
	}
	
	BroadElement getParent() {
		return this.parent;
	}
	
	int getOrder() {
		return this.order;
	}
	
	int getId() {
		return this.id;
	}
	
	boolean isFull() {
		return(this.breadth == this.refs);
	}
	
	boolean isEmpty() {
		return(this.refs == 0);
	}
	
	int getLevel() {
		int result = 0;
		BroadElement prev = this.parent;
		while(prev != null) {
			result++;
			prev = prev.parent;
		}
		return result;
	}
	
	BroadElement getRef(int index) {
		if(index < 1 || index > this.breadth) {
			System.out.println("getRef - illegal args, index="+index);
			System.exit(1);
		}
		switch (index) {
		case 1:
			return this.next1;
		case 2:
			return this.next2;
		case 3:
			return this.next3;
		case 4:
			return this.next4;
		case 5:
			return this.next5;
		case 6:
			return this.next6;
		case 7:
			return this.next7;
		case 8:
			return this.next8;
		case 9:
			return this.next9;
		case 10:
			return this.next10;
		}
		return null;
	}

	void setRef(BroadElement node, int index) {
		if(index < 1 || index > this.breadth) {
			System.out.print("setNext - illegal args, index="+index);
			System.exit(1);
		}
		switch (index) {
		case 1:
			if(this.next1 != null) {
				reportRefill(this.next1, node);
			}
			this.next1 = node;
			break;
		case 2:
			if(this.next2 != null) {
				reportRefill(this.next2, node);
			}
			this.next2 = node;
			break;
		case 3:
			if(this.next3 != null) {
				reportRefill(this.next3, node);
			}
			this.next3 = node;
			break;
		case 4:
			if(this.next4 != null) {
				reportRefill(this.next4, node);
			}
			this.next4 = node;
			break;
		case 5:
			if(this.next5 != null) {
				reportRefill(this.next5, node);
			}
			this.next5 = node;
			break;
		case 6:
			if(this.next6 != null) {
				reportRefill(this.next6, node);
			}
			this.next6 = node;
			break;
		case 7:
			if(this.next7 != null) {
				reportRefill(this.next7, node);
			}
			this.next7 = node;
			break;
		case 8:
			if(this.next8 != null) {
				reportRefill(this.next8, node);
			}
			this.next8 = node;
			break;
		case 9:
			if(this.next9 != null) {
				reportRefill(this.next9, node);
			}
			this.next9 = node;
			break;
		case 10:
			if(this.next10 != null) {
				reportRefill(this.next10, node);
			}
			this.next10 = node;
			break;
		}
		this.refs++;
	}
	
	private void reportRefill(BroadElement oldNode, BroadElement newNode) {
		oldNode.dump("reportRefill, oldNode");
		newNode.dump("reportRefill, newNode");
		System.exit(1);
	}
	
	void dump(String message) {
		System.out.print(message+": id="+this.id);
		System.out.print(", parentID="+this.parent.id);
		System.out.print(", level="+this.getLevel());
		System.out.print(", order="+this.order);
		System.out.println(", refs="+this.refs);
	}

	public int getRefs() {
		return this.refs;
	}
}
